﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;
using System.Data;
/// <summary>
///About 的摘要说明
/// </summary>
public class SetWeb
{
    public SetWeb()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    public OleDbDataAdapter GetNewsAll()
    {

        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbDataAdapter odp = new OleDbDataAdapter("select * from [Webset]  Order BY Wid DESC", myconn);
        return odp;
    }

    public int GetNewsCount()
    {
        int s = 0;
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select Count(*) from [Webset] ", myconn);

        try
        {
            myconn.Open();
            s = int.Parse(mycmd.ExecuteScalar().ToString());
        }

        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }

        return s;
    }
    public OleDbDataReader GetNewsConn(int Wid)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [Webset] where Wid=" + Wid + " ", myconn);
        OleDbDataReader dr = null;
        try
        {
            myconn.Open();
            dr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }

        return dr;
    }
    public int ADDNews(string Wkey, string Wvalue)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("Insert into Webset(Wkey,Wvalue) Values ('" + Wkey + "',@Wvalue)", myconn);
        mycmd.Parameters.AddWithValue("@Wvalue", Wvalue);
        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
    public int UPPNews(string Wkey, string Wvalue, int Wid)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("UPdate [Webset] Set Wkey=@Wkey,Wvalue=@Wvalue where Wid=" + Wid + "", myconn);
        mycmd.Parameters.AddWithValue("@Wkey", Wkey);
        mycmd.Parameters.AddWithValue("@Wvalue", Wvalue);
        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
    public DataSet GetNewsPage(int SPage1, int SPage2, string Sname)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [Webset] Order By Wid DESC", myconn);
        OleDbDataAdapter dr = new OleDbDataAdapter(mycmd);
        DataSet da = new DataSet();
        try
        {
            myconn.Open();
            dr.Fill(da, SPage1, SPage2, Sname);
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return da;
    }
    //删除数据
    public int DelNews(int Wid)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("delete from [Webset] where Wid = @Wid ", myconn);
        mycmd.Parameters.AddWithValue("@Wid", Wid);
        int nResult = -1;
        try
        {
            myconn.Open();
            nResult = mycmd.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return nResult;
    }
}